package com.递归2;

import java.util.HashMap;
import java.util.Map;

/**
 * 递归实现n^k(n的k次幂)
 * 如当n=2,k=3, 结算结果为:2*2*2=8
 */
public class n的k次幂 {
    public static class Solution {
        public static int mi(int n, int k) {
            if (k == 0) {
                return 1;
            }
            if (k == 1) {
                return n;
            }
            return n * mi(n, k - 1);
        }

        public static Map<Double, Double> cache = new HashMap<>();
        public static Double[] doubles;

        public static double myPow(double x, int n) {

            boolean zhengshu = true;
          if(n<0){
              zhengshu = false;
              n *=-1;
          }
          doubles = new Double[n*(n+1)];
            double number = myPow2(x, n);
          return zhengshu ? number : 1.0/number;

        }

        public static double myPow2(double x, int n) {
            if (n == 0) {
                return 1.0;
            }
            if (n == 1) {
                return x;
            }
            double number =   x * myPow(x, n - 1);
            doubles[n] = number;
//            cache.put(x, x * myPow(x, n - 1));
            return doubles[n];
        }

        public static void main(String[] args) {
           // System.out.println(mi(2, 10));
//            System.out.println(1.0/4.0);
            System.out.println(myPow(2,3));
//             Double[] doubless = new Double[2+1];
//             doubless[2]  = 4.0;
//            System.out.println(doubless[2]);
        }

    }
}
